"""
封装报告模块
"""

import datetime
import os.path

from BeautifulReport import BeautifulReport as bfReport
from HTMLTestRunner import HTMLTestRunner


def report(suite, filename, report_dir, theme='theme_default', title=None, description=None, _type='br'):
    """
    执行用例并生成报告
    :param ts:测试套件
    :param filename:报告文件名
    :param report_dir:报告文件夹
    :param theme:主题，仅支持BeautifulReport
    :param title:报告标题，仅支持HTMLTestRunner
    :param description:报告描述
    :param _type: 默认值为br,生成仅支持BeautifulReport的报告
    :return:
    """
    # 1、生成时间前缀
    now_time = datetime.datetime.now().strftime('%Y_%m_%d %H：%M：%S')
    # 2、拼接到报告文件名
    filename = '{}_{}'.format(now_time, filename)
    print(filename)
    if _type == 'br':
        # 生成BeautifulReport的报告
        run = bfReport(suite)
        run.report(filename=filename,
                   description=description, report_dir=report_dir, theme=theme)
    else:
        # 生成仅支持HTMLTestRunner的报告
        with open(os.path.join(report_dir, filename), 'wb') as f:
            runer = HTMLTestRunner(f, title=title, description=description)
            runer.run(suite)
